import { useState, useCallback } from 'react';
import { Message } from '@arco-design/web-react';
export default function useFetchData(fetchData, initialParams) {
  //console.log("initialParams", initialParams);

  const [data, setData] = useState([]);

  const [loading, setLoading] = useState(false);

  const getData = useCallback(
    async (searchParams = initialParams) => {
      try {
        const { ...restSearchParams } = searchParams;
        const obj = {
          ...restSearchParams,
        };

        setLoading(true);

        const res = await fetchData({ ...obj });

        setData(res.Result.Datas);

        setLoading(false);
      } catch (error) {
        setLoading(false);
        Message.error('列表获取失败，请刷新重试');
      }
    },
    [fetchData, initialParams]
  );

  return { data, loading, getData };
}
